﻿
using System;
using System.Web;
using System.Collections;
using System.Text;
using System.Data;
using System.Xml;
using System.Web.SessionState;
using System.Web.Services;
using Gift.WebApp;
using Gift.DAL;
using Gift.Model;

namespace WebApp.cn.ashx
{
    public class specificationsHandler : IHttpHandler, IRequiresSessionState
    {

        readonly BasePage thePage = new BasePage();

        /// <summary>
        /// 您将需要在您网站的 web.config 文件中配置此处理程序，
        /// 并向 IIS 注册此处理程序，然后才能进行使用。有关详细信息，
        /// 请参见下面的链接: http://go.microsoft.com/?linkid=8101007
        /// </summary>
        #region IHttpHandler Members

        public bool IsReusable
        {
            // 如果无法为其他请求重用托管处理程序，则返回 false。
            // 如果按请求保留某些状态信息，则通常这将为 false。
            get { return true; }
        }
        // Methods
        private static string GetGoodsEntitys(DataTable dtGes, string a, bool rs)
        {
            foreach (DataRow row in dtGes.Rows)
            {
                a = a + (!rs ? row["goodsEntitys"] : ("," + row["goodsEntitys"]));
                rs = true;
            }
            return a;
        }

        public void ProcessRequest(HttpContext context)
        {

            try
            {
                int num;
                string str = context.Request.Form["specifications"];
                string str2 = context.Request.Form["specificationsValue"];
                ArrayList list = ArrayList.Adapter(context.Request.Form["SpecificationsList"].Split(new char[] { ',' }));
                ArrayList list2 = ArrayList.Adapter(context.Request.Form["SpecificationsValueList"].Split(new char[] { ',' }));
                for (num = 0; num < list.Count; num++)
                {
                    if (list[num].ToString() == str)
                    {
                        list2[num] = str2;
                    }
                }
                string str3 = context.Request.Form["goods"];
                DataTable table = BaseFactory.theGoodsEntitysSpecifications.FrontDeskGetGoodsEntitysSpecificationsByGoods(Convert.ToInt32(context.Request.Form["goods"]));
                DataTable dtGes = new DataTable();
                bool rs = false;
                string goodsEntitys = "";
                dtGes = BaseFactory.theGoodsEntitysSpecifications.GetGoodsEntitysBySpecificationsValueAndGoodsAndGoodsEntitys(Convert.ToInt32(str3), Convert.ToInt32(str2), goodsEntitys);
                DataTable table3 = new DataTable();
                if (dtGes.Rows.Count > 1)
                {
                    table3 = dtGes;
                    goodsEntitys = "";
                    goodsEntitys = GetGoodsEntitys(dtGes, goodsEntitys, rs);
                    for (num = 0; num < list2.Count; num++)
                    {
                        if (list[num].ToString() != str)
                        {
                            dtGes = BaseFactory.theGoodsEntitysSpecifications.GetGoodsEntitysBySpecificationsValueAndGoodsAndGoodsEntitys(Convert.ToInt32(str3), Convert.ToInt32(list2[num]), goodsEntitys);
                            if (dtGes.Rows.Count > 0)
                            {
                                if (dtGes.Rows.Count == 1)
                                {
                                    break;
                                }
                                goodsEntitys = "";
                                goodsEntitys = GetGoodsEntitys(dtGes, goodsEntitys, rs);
                            }
                            else
                            {
                                dtGes = table3;
                                break;
                            }
                        }
                    }
                }
                int id = Convert.ToInt32(dtGes.Rows[0]["goodsEntitys"]);
                if (id != 0)
                {
                    DataTable goodsEntitysById = BaseFactory.theGoodsEntitys.GetGoodsEntitysById(id);
                    if (goodsEntitysById.Rows.Count > 0)
                    {
                        DataRow current;
                        string str5 = "货品编号：" + goodsEntitysById.Rows[0]["goodsArtNo"].ToString();
                        string str6 = "会员价：<strong>" + string.Format("{0:C}", Convert.ToDecimal(goodsEntitysById.Rows[0]["salePrice"])) + "</strong> <del>市场价：" + string.Format("{0:C}", Convert.ToDecimal(goodsEntitysById.Rows[0]["marketPrice"])) + "</del>";
                        string str7 = goodsEntitysById.Rows[0]["inventory"].ToString();
                        string str8 = "";
                        foreach (string str9 in list2)
                        {
                            str8 = str8 + ((str8 == "") ? str9 : ("," + str9));
                        }
                        StringBuilder builder = new StringBuilder();
                        builder.AppendLine("已选择：");
                        bool flag2 = false;
                        StringBuilder builder2 = new StringBuilder();
                        ArrayList list3 = new ArrayList();
                        ArrayList list4 = new ArrayList();
                        ArrayList list5 = new ArrayList();
                        IEnumerator enumerator = table.Rows.GetEnumerator();

                        while (enumerator.MoveNext())
                        {
                            current = (DataRow)enumerator.Current;
                            if (current["goodsEntitys"].ToString() == id.ToString())
                            {
                                builder.AppendLine((!flag2 ? "" : "&nbsp;&nbsp;") + current["specificationsValueName"].ToString());
                                flag2 = true;
                                list5.Add(current["specificationsValue"].ToString());
                            }
                        }

                        for (num = 0; num < table.Rows.Count; num++)
                        {
                            current = table.Rows[num];
                            if (Convert.ToInt32(current["specificationsType"]) == 0)
                            {
                                if (!list3.Contains(Convert.ToInt32(current["specifications"])))
                                {
                                    builder2.AppendLine(((num != 0) ? "<br />" : "") + current["specificationsName"] + "：");
                                    list3.Add(Convert.ToInt32(current["specifications"]));
                                }
                                if (!list4.Contains(Convert.ToInt32(current["specificationsValue"])))
                                {
                                    string str10 = current["goodsEntitys"].ToString();
                                    if (list5.Contains(current["specificationsValue"].ToString()))
                                    {
                                        builder2.AppendLine(string.Concat(new object[] { "<strong  class=\"selectStrong\" onclick='chooseSpecifications(\"", current["goodsEntitys"], "\",\"", current["specifications"], "\",\"", current["specificationsValue"], "\");'>", current["specificationsValueName"], "</strong>" }));
                                    }
                                    else
                                    {
                                        builder2.AppendLine(string.Concat(new object[] { "<strong class=\"strong\" onclick='chooseSpecifications(\"", current["goodsEntitys"], "\",\"", current["specifications"], "\",\"", current["specificationsValue"], "\");'>", current["specificationsValueName"], "</strong>" }));
                                    }
                                    list4.Add(Convert.ToInt32(current["specificationsValue"]));
                                }
                            }
                            else
                            {
                                if (!list3.Contains(Convert.ToInt32(current["specifications"])))
                                {
                                    builder2.AppendLine(((num != 0) ? "<br />" : "") + current["specificationsName"] + "：");
                                    list3.Add(Convert.ToInt32(current["specifications"]));
                                }
                                if (!list4.Contains(Convert.ToInt32(current["specificationsValue"])))
                                {
                                    string str11 = current["goodsEntitys"].ToString();
                                    if (list5.Contains(current["specificationsValue"].ToString()))
                                    {
                                        builder2.AppendLine(string.Concat(new object[] { "<span onclick='chooseSpecifications(\"", current["goodsEntitys"], "\",\"", current["specifications"], "\",\"", current["specificationsValue"], "\");'><img class=\"selectStrong\" src=\"", current["specificationsValueImage"], "\" alt=\"", current["specificationsValueName"], "\" /></span>" }));
                                    }
                                    else
                                    {
                                        builder2.AppendLine(string.Concat(new object[] { "<span onclick='chooseSpecifications(\"", current["goodsEntitys"], "\",\"", current["specifications"], "\",\"", current["specificationsValue"], "\");'><img class=\"strong\" src=\"", current["specificationsValueImage"], "\" alt=\"", current["specificationsValueName"], "\" /></span>" }));
                                    }
                                    list4.Add(Convert.ToInt32(current["specificationsValue"]));
                                }
                            }
                        }
                        context.Response.Write(string.Concat(new object[] { str5, "+", str6, "+", str7, "+", str8, "+", builder.ToString(), "+", builder2.ToString(), "+", id }));
                    }
                }
            }
            catch
            {
                context.Response.Write("");
            }
        }

        #endregion
    }
}
